package com.linktrip.devicemanager.server;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.slf4j.Logger;
import java.io.InputStream;
import org.slf4j.LoggerFactory;
import java.io.FileOutputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.util.concurrent.ExecutorService;
import org.springframework.stereotype.Service;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import com.linktrip.devicemanager.entity.DeviceInfo;
import com.linktrip.devicemanager.task.ExcelReadTask;

/**
 * @author : CaineZhu E-mail:CaineZhu@me.com
 * @version : V1.0
 * @创建时间 ： 2017年6月7日 下午2:46:18
 * @类说明 : 设备服务类
 */

@Service
public class OfficeService {

	Logger logger = LoggerFactory.getLogger(OfficeService.class);
	ExecutorService executor = Executors.newCachedThreadPool();
	
	// 公司内部业务系统使用
	// 解析流
	public List<DeviceInfo> getDeviceInfos(InputStream is) throws Exception {
		List<DeviceInfo> list = null;
		Workbook wb = WorkbookFactory.create(is);
		is.close();

		if (null == wb)
			return null;
		ExcelReadTask callable = new ExcelReadTask(wb);
		FutureTask<List<DeviceInfo>> task = new FutureTask<>(callable);
		executor.submit(task);
		list = task.get();
		return list;
	}

	// 公司内部业务系统使用
	// 读取数据
	public boolean createBikeInfos(List<DeviceInfo> bikes, File tempFile) throws Exception {

		try {
			XSSFWorkbook wb = new XSSFWorkbook();
			Sheet sheet = wb.createSheet("sheet1");
			// 创建头
			Row headerRow = sheet.createRow(0);
			Cell headerCell = headerRow.createCell(0);
			headerCell.setCellValue("批次编号");

			headerCell = headerRow.createCell(1);
			headerCell.setCellValue("单车编号");

			headerCell = headerRow.createCell(2);
			headerCell.setCellValue("单车类型");

			headerCell = headerRow.createCell(3);
			headerCell.setCellValue("锁编号");

			headerCell = headerRow.createCell(4);
			headerCell.setCellValue("锁id");

			headerCell = headerRow.createCell(5);
			headerCell.setCellValue("手机号码");

			headerCell = headerRow.createCell(6);
			headerCell.setCellValue("sim卡号");

			headerCell = headerRow.createCell(7);
			headerCell.setCellValue("备注");

			headerCell = headerRow.createCell(8);
			headerCell.setCellValue("渠道ID");

			for (int i = 1; i <= bikes.size(); i++) {
//				DeviceInfo bike = bikes.get(i - 1);
//				Row row = sheet.createRow(i);
//
//				Cell cell = row.createCell(0);
//				String batch = bike.getBatch();
//				if (!StringTool.isEmpty(batch))
//					cell.setCellValue(batch);
//
//				cell = row.createCell(1);
//				String bikeNo = bike.getBikeNo();
//				if (!StringTool.isEmpty(bikeNo))
//					cell.setCellValue(bikeNo);
//
//				cell = row.createCell(2);
//				int type = bike.getType();
//				if (!StringTool.isEmpty(bikeNo))
//					cell.setCellValue(type);
//
//				cell = row.createCell(3);
//				String id = String.valueOf(bike.getId());
//				if (!StringTool.isEmpty(id))
//					cell.setCellValue(id);
//
//				cell = row.createCell(4);
//				String deviceNo = bike.getDeviceNo();
//				if (!StringTool.isEmpty(deviceNo))
//					cell.setCellValue(deviceNo);
//
//				cell = row.createCell(5);
//				String phoneNumber = bike.getPhoneNumber();
//				if (!StringTool.isEmpty(phoneNumber))
//					cell.setCellValue(phoneNumber);
//
//				cell = row.createCell(6);
//				String simNo = bike.getSimNo();
//				if (!StringTool.isEmpty(simNo))
//					cell.setCellValue(simNo);
//
//				cell = row.createCell(7);
//				String remark = bike.getRemark();
//				if (!StringTool.isEmpty(remark))
//					cell.setCellValue(bike.getRemark());
//
//				cell = row.createCell(8);
//				int channelId = bike.getChannelId();
//				if (!StringTool.isEmpty(channelId))
//					cell.setCellValue(bike.getChannelId());
			}
			FileOutputStream fos = new FileOutputStream(tempFile);
			wb.write(fos);
			fos.flush();
			fos.close();
			wb.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static void main(String[] args) throws Exception {
		OfficeService office = new OfficeService();
		List<DeviceInfo> list = office.getDeviceInfos(
				new FileInputStream(new File("C:/Users/Thinkpad/Desktop/0712 工厂生产G1000lite chaopai 200 把锁后台信息.xlsx")));
		for (DeviceInfo bike : list) {
			System.out.println(bike.toString());
		}

		//office.createBikeInfos(list, new File("d:/temp/201707071338.xlsx"));
	}
}
